package com.kiven.algorithm;

import java.lang.reflect.Array;
import java.util.*;

public class Test {
	public static void main(String[] args) {
		
		int[] periods = {2,3,4,7,8,9,10,11,12,13,15,17,18,19,20};
		int[] periods1 = {18,2,7,8,13,15,9,3,11,20};
		//2==3==7==8==9==11==13==15==18==20==
		System.out.println(new Test().getContinueCountByArray(periods1));
//		System.out.println(new Test().getContinueCountBySet(periods1));
		
		System.out.println(1>>1);
	}
	
	/**
	 * 获得数组中连续数字的最大值
	 * 
	 * @param periods 存放各期租金中纯在延缴或者逾期的期号数据
	 * @return
	 */
	public int getContinueCountByArray(int[] periods) {
		// 当数组没有值是返回0
		if (periods.length <= 0) {
			return 0;
		}
		
		// 为了保证程序的健壮性，必须先对数组进行排序
		Arrays.sort(periods);
		// 存放连续延缴期数的最大值
		int maxContinueCount = 1;
		for(int i=0,size=periods.length; i<size-1; i++) {
			// 每次循环中存放最长连续期数的临时数据
			int temp = 1;
			// 数组的前置下标
			int temp0 = i;
			// 数组的后置下标
			int temp1 = i + 1;
			while (temp0 <size-1 && temp1 < size && periods[temp0] + 1 == periods[temp1] ) {
				temp ++;
				temp0++;
				temp1++;
			}
			
			if (temp > maxContinueCount) {
				maxContinueCount = temp;
			}
		}
		
		return maxContinueCount;
	}
	
	public String getContinueCountBySet(int[] periods) {
//		Collection coll = periods.q
//		set.addAll(Arrays.sort(periods));
		StringBuilder sb = new StringBuilder();
		Arrays.sort(periods);
		for (int i : periods) {
			sb.append(i + "==");
		}
		
		return sb.toString();
	}
}
